﻿// -----------------------------------------------------------------------
// <copyright file="MembershipFactory.cs" company="CompDJ">
//     Copyright (c) CompDJ. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace CompDJ.Web.Abstractions
{
    using System;

    /// <summary>Membership factory.</summary>
    public class MembershipFactory
    {
        /// <summary>Context holder field.</summary>
        private static IMembership context = null;

        /// <summary>Context creation holder field.</summary>
        private static Func<IMembership> creatorFunction = null;

        /// <summary>Gets the Current property.</summary>
        public static IMembership Current
        {
            get
            {
                if (context != null)
                {
                    return context;
                }

                if (creatorFunction != null)
                {
                    return creatorFunction.Invoke();
                }

                return new MembershipAdapter();
            }
        }

        /// <summary>Sets the context.</summary>
        /// <param name="instance">Instance to use</param>
        /// <remarks>This method is strictly for testing.</remarks>
        public static void SetContext(IMembership instance)
        {
            context = instance;
        }

        /// <summary>Sets the function for creation of context.</summary>
        /// <param name="creator">Instance to use</param>
        /// <remarks>This method is strictly for testing.</remarks>
        public static void SetCreationFunction(Func<IMembership> creator)
        {
            creatorFunction = creator;
        }
    }
}
